package org.openkanban.client.ui.presenter;

import org.openkanban.client.ui.board.WhiteBoardView;
import org.openkanban.client.util.RootPanel;
import org.openkanban.shared.dto.KanbanDto;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.smartgwt.client.util.JSON;
import com.smartgwt.client.util.SC;

/**
 * Callback that handles move kanban to activity request
 */
public class MoveKanbanToActivityRequestCallback implements RequestCallback {

    private Long sourceActivityId;
    private Long targetActivityId;

    @Override
    public void onResponseReceived(Request request, Response response) {
	KanbanDto kanbanDto = new KanbanDto(response.getText());
	if (kanbanDto.getFehlerKz() > 0) {
	    SC.say(kanbanDto.getFehlerText());
	} else {
	    RootPanel.main.getFooter().updateBoardHistoryData();
	    WhiteBoardView currentWhiteBoardView = RootPanel.main.getBoard().getWhiteBoardView();
	    currentWhiteBoardView.publishKanbanMove(JSON.decode("{ \"kanbanId\":\"" + kanbanDto.getKanbanId().toString()
		    + "\", \"sourceActivityId\":\"" + sourceActivityId.toString() + "\", \"targetActivityId\":\""
		    + targetActivityId.toString() + "\" }"));
	}
    }

    @Override
    public void onError(Request request, Throwable exception) {
    }

    public void setSourceActivityId(Long sourceActivityId) {
	this.sourceActivityId = sourceActivityId;
    }

    public void setTargetActivityId(Long targetActivityId) {
	this.targetActivityId = targetActivityId;
    }

}
